package com.aistar.servlet;

import com.aistar.Job;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Test5Servlet",urlPatterns = "/test5")
public class Test5Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jobname=  request.getParameter("fjob");                   // <input name="fjob">
        String city=  request.getParameter("fcity");                     // <input name="fcity">
        float salary= Float.valueOf( request.getParameter("fsalary"));   // <input name="fsalary">

        System.out.println("job:"+jobname +",city:"+city+"salary:"+salary);


        Job job = new Job(jobname,city,salary);
        String josnString = JSONObject.toJSONString(job);  // 简单的Bean转为JSON

        response.setContentType("text/html;charset=utf-8"); //注意此处要将响应中的JSON数据 转码为utf-8,否则会中文乱码
        response.getWriter().print(josnString);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
